package net.xiangcaowuyu.shiro.config;

import net.xiangcaowuyu.shiro.entity.ShiroPermission;
import net.xiangcaowuyu.shiro.entity.ShiroRole;
import net.xiangcaowuyu.shiro.entity.ShiroUser;
import net.xiangcaowuyu.shiro.entity.ShiroUserVO;
import net.xiangcaowuyu.shiro.service.ShiroService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * @author laughing
 * @date 2020/10/11
 * @site https://www.lisen.org
 */
public class MyAuthenRealm extends AuthorizingRealm {

    @Resource
    ShiroService shiroService;

    /**
     * 认证
     *
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        String userCode = token.getUsername();
        ShiroUser shiroUser = shiroService.findUserByCode(userCode);
        if (shiroUser == null) {
            throw new AuthenticationException("账户不存在");
        }
        //自定义盐值
//        ByteSource salt = ByteSource.Util.bytes(AuthenConst.SALT);
//        String password = new SimpleHash(AuthenConst.HASH_ALGORITHM_NAME, new String(token.getPassword()), AuthenConst.SALT, AuthenConst.HASH_INTERACTIONS).toHex();
        if (!new String(token.getPassword()).equals(shiroUser.getPassword())) {
            throw new IncorrectCredentialsException("账户密码不正确");
        }
//        Subject subject = SecurityUtils.getSubject();
//        ShiroUserVO shiroUserVO = new ShiroUserVO();
//        BeanUtils.copyProperties(shiroUserVO,shiroUser);
//        shiroUserVO.setSessionId(subject.getSession().getId().toString());
        return new SimpleAuthenticationInfo(userCode, shiroUser.getPassword(), getName());
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        if (StringUtils.isEmpty(principalCollection)) {
            return null;
        }
        String userCode = principalCollection.getPrimaryPrincipal().toString();
        ShiroUser shiroUser = shiroService.findUserByCode(userCode);
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        for (ShiroRole shiroRole : shiroUser.getShiroRoleSet()) {
            simpleAuthorizationInfo.addRole(shiroRole.getRoleCode());
            for (ShiroPermission shiroPermission : shiroRole.getShiroPermissionSet()) {
                simpleAuthorizationInfo.addStringPermission(shiroPermission.getPermissionCode());
            }
        }
        return simpleAuthorizationInfo;
    }
}
